package PVZ;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Game {
    static Boolean ifGame=true;
    static Pictrue1 p1=new Pictrue1();
    public static int type=10;
    static BufferedImage rungame;
    static BufferedImage SeedBank;
    public static int i=0,s=200;
    public static String sun=""+s+"";
    //定义二维数组,第一个格子中心坐标(380,160);间隔108,125;
    //0空，1向日葵，2豌豆射手，3坚果墙，4双重射手,5樱桃炸弹,6土豆地雷
    public static int X_RATE=108;
    public static int Y_RATE=125;
    public static int X_OFFSET=380,Y_OFFSET=160;
    public static int SLECT_X=-1,SLECT_Y=-1;
    //共5行9列
    public static int[][]plants=new int[9][5];
    public static int[][]state=new int[9][5];
    public static int[]ifZ=new int[5];//此行是否有僵尸
    public static int wei=0;
    public static int lkl=0;
    public static int Cost;//总花费的阳光
    public static int[][]ifPeas=new int[1920][800];//此处是否有豌豆
    public static int[][]ifplants=new int[1920][800];//此处是否有植物
    public static int[][]ifZ0=new int[1920][800];//此处是否有僵尸
    public static int[][]Ztype=new int[1920][800];//此处僵尸的类型
    //添加植物卡片
    static ImageIcon SunFlower=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\SunFlower0.png");
    static ImageIcon Peashooter=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\Peashooter0.png");
    static ImageIcon WallNut0=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\WallNut0.png");
    static ImageIcon Repeater0=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\Repeater0.png");
    static ImageIcon CherryBomb0=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\CherryBomb0.png");
    static ImageIcon PotatoMine0=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\PotatoMine0.png");
    static JButton SunF=new JButton(SunFlower);
    static JButton Peas=new JButton(Peashooter);
    static JButton WallN=new JButton(WallNut0);
    static JButton Reap=new JButton(Repeater0);
    static JButton Cherry=new JButton(CherryBomb0);
    static JButton Potato=new JButton(PotatoMine0);
    //创建JFrame界面,开始界面
    static JFrame f = new JFrame("植物大战僵尸");
    public static void init1(){
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        p1.setVisible(true);
        SunF.setBounds(300,7,SunFlower.getIconWidth(),SunFlower.getIconHeight());
        //添加按钮监听
        SunF.addActionListener(e -> {
            if(s>=50){
                if(type!=1){
                    type=1;
                    s=s-50;
                    sun=""+s+"";
                }
            }
        });
        Peas.setBounds(360,7,Peashooter.getIconWidth(),Peashooter.getIconHeight());
        Peas.addActionListener(e -> {
            if(s>=100){
                if(type!=2) {
                    s = s - 100;
                    sun = "" + s + "";
                    type = 2;
                }
            }
        });
        WallN.setBounds(420,7,WallNut0.getIconWidth(),WallNut0.getIconHeight());
        WallN.addActionListener(e -> {
            if(s>=50) {
                if (type != 3) {
                    type = 3;
                    s = s - 50;
                    sun = "" + s + "";
                }
            }
        });
        Reap.setBounds(480,7,Repeater0.getIconWidth(),Repeater0.getIconHeight());
        Reap.addActionListener(e -> {
            if(s>=200) {
                if (type != 4) {
                    type = 4;
                    s = s - 200;
                    sun = "" + s + "";
                }
            }
        });
        Cherry.setBounds(540,7,CherryBomb0.getIconWidth(),CherryBomb0.getIconHeight());
        Cherry.addActionListener(e -> {
            if(s>=150) {
                if (type != 5) {
                    type = 5;
                    s = s - 150;
                    sun = "" + s + "";
                }
            }
        });
        Potato.setBounds(600,7,PotatoMine0.getIconWidth(),PotatoMine0.getIconHeight());
        Potato.addActionListener(e -> {
            if(s>=25){
                if(type!=6) {
                    type = 6;
                    s = s - 25;
                    sun = "" + s + "";
                }
            }
        });
        //处理鼠标点击
        p1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(type!=0) {
                    int XPos = (e.getX() - X_OFFSET) / X_RATE;
                    int YPos = (e.getY() - Y_OFFSET) / Y_RATE;
                    if (state[XPos][YPos] != 1) {//1代表有此处有植物了
                        plants[XPos][YPos] = type;
                        p1.repaint();
                        type = 0;
                    }
                }
            }
            //鼠标移动
           /*public void mouseExited(MouseEvent e){
                SLECT_X=-1;
                SLECT_Y=-1;
                //p1.repaint();
            }*/
        });
        f.add(SunF);
        f.add(Peas);
        f.add(WallN);
        f.add(Reap);
        f.add(Cherry);
        f.add(Potato);
        f.add(p1);
        p1.setPreferredSize(new Dimension(1920,800));
        //加入植物
        try {
            rungame =ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\src\\PVZ\\Running  .jpg"));
            SeedBank=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\SeedBank.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            p1.repaint();
            f.pack();
        }
        //僵尸入场
        try {
            Pictrue1.Z1 = ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z1.png"));
            Pictrue1.Z1t=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z1attack.png"));
            Pictrue1.Zd=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Zdie.png"));
            Pictrue1.Z2=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z2.png"));
            Pictrue1.Z2t=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z2attack.png"));
            Pictrue1.Z3=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z3.png"));
            Pictrue1.Z3t=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z3attack.png"));
            Pictrue1.Z10=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z1\\zombie10.png"));
            Pictrue1.Z11=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z1\\zombie11.png"));
            Pictrue1.Z12=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z1\\zombie12.png"));
            Pictrue1.Z13=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z1\\zombie13.png"));
            Pictrue1.Z14=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z1\\zombie14.png"));
            Pictrue1.Z20=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z2\\zombie20.png"));
            Pictrue1.Z21=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z2\\zombie21.png"));
            Pictrue1.Z22=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z2\\zombie22.png"));
            Pictrue1.Z23=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z2\\zombie23.png"));
            Pictrue1.Z24=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z2\\zombie24.png"));
            Pictrue1.Z30=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z3\\zombie30.png"));
            Pictrue1.Z31=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z3\\zombie31.png"));
            Pictrue1.Z32=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z3\\zombie32.png"));
            Pictrue1.Z33=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z3\\zombie33.png"));
            Pictrue1.Z34=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Zombies\\Z3\\zombie34.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        getZ getZ=new getZ();
        Thread thread3=new Thread(getZ);
        thread3.start();
        //僵尸进场后刷新游戏画面
        ReGame reGame=new ReGame();
        Thread thread4=new Thread(reGame);
        thread4.start();
        //有僵尸出现自动换图
        //RePicture.ifRe=true;
        RePicture rePicture=new RePicture();
        Thread thread01=new Thread(rePicture);
        thread01.start();
    }
    static ImageIcon Sun=new ImageIcon("D:\\Morven\\IdeaProjects\\PVZ\\picture\\20.png");
    static JButton JBsun;
    static int i1;
    //添加阳光
    static int i2;
    public static void setSun(int y){
        //如果不及时收集向日葵生产的前一个阳光，后一个阳光产生时前一个将自动收集
        if(wei==0){
            if(i1!=0){
            p1.remove(JBsun);
            s=s+25;
            i1=0;
            }
            i1=1;
            JBsun=new JButton(Sun);
            p1.add(JBsun);
            p1.repaint();
        }else
        {
            wei=0;
            if(i1!=0){
                p1.remove(JBsun);
                s=s+25;
                i1=0;
            }
            i1=1;
            JBsun=new JButton(Sun);
            p1.add(JBsun);
            p1.repaint();
        }
    }
    public void importSun(int x, int y){
        lkl=1;
        setSun(y);
        i2=y;
        JBsun.setBounds(x,y,Sun.getIconWidth(),Sun.getIconHeight());
        JBsun.addActionListener(e -> {
            s=s+25;
            sun=""+s+"";
            wei=1;
            p1.remove(JBsun);
            p1.repaint();
        });
    }
    static class Pictrue1 extends JPanel{
        static Pictrue1 pictrue1=new Pictrue1();
        BufferedImage SunFlower;
        BufferedImage Peashooter;
        BufferedImage WallNut;
        BufferedImage Reshooter;
        BufferedImage CherryBomb;
        BufferedImage PotatoMine;
        static BufferedImage Pea;
        static BufferedImage Z1;//1
        static BufferedImage Z2;//2
        static BufferedImage Z3;//3
        static BufferedImage Z1t;//19
        static BufferedImage Z2t;//29
        static BufferedImage Z3t;//39
        static BufferedImage Zd;//99
        static BufferedImage Z10;
        static BufferedImage Z11;
        static BufferedImage Z12;
        static BufferedImage Z13;
        static BufferedImage Z14;
        static BufferedImage Z20;
        static BufferedImage Z21;
        static BufferedImage Z22;
        static BufferedImage Z23;
        static BufferedImage Z24;
        static BufferedImage Z30;
        static BufferedImage Z31;
        static BufferedImage Z32;
        static BufferedImage Z33;
        static BufferedImage Z34;
        public int PBlood=0;
        public static int ypian;
        public int PeaType;
        public int PeaX;
        public int PeaY;
        public void DrawPea(int x1,int y1,int type){
            PeaType=type;
            PeaX=x1;
            PeaY=y1;
           /* Graphics g=getGraphics();
            g.drawImage(Pea,x1,y1,null);
            if(type==2){
                g.drawImage(Pea,x1-40,y1,null);
            }*/
        }
        public void DrawZ(int x2,int y2,int type,int re){
            Graphics g=getGraphics();
            if(type==1){
                if(ifZ0[x2][y2]==1){
                    Ztype[x2][y2]=10;
                //g.drawImage(Z1,x2,y2-40,null);
                /*if(re==0){
                    RePicture.ifRe=true;
                    RePicture rePicture=new RePicture(x2,y2,1);
                    Thread thread01=new Thread(rePicture);
                    thread01.start();
                }*/
                }
                else if(ifZ0[x2][y2]==0){
                    //RePicture.ifRe=false;
                    //g.drawImage(Zd,x2,y2-40,null);
                    Ztype[x2][y2]=99;
                }else if(ifZ0[x2][y2]==2){
                    //RePicture.ifRe=false;
                    //g.drawImage(Z1t,x2,y2-40,null);
                    Ztype[x2][y2]=19;
                }
            }
            if(type==2){
                if(ifZ0[x2][y2]==1){
                    //g.drawImage(Z2,x2,y2-40,null);
                    Ztype[x2][y2]=20;
                }
                else if(ifZ0[x2][y2]==0){
                    //g.drawImage(Zd,x2,y2-40,null);
                    Ztype[x2][y2]=99;
                }else if(ifZ0[x2][y2]==2){
                    //g.drawImage(Z2t,x2,y2-40,null);
                    Ztype[x2][y2]=29;
                }
                }
            if(type==3){
                if(ifZ0[x2][y2]==1){
                    //g.drawImage(Z3,x2,y2-40,null);
                    Ztype[x2][y2]=30;
                }
                else if(ifZ0[x2][y2]==0){
                    //g.drawImage(Zd,x2,y2-40,null);
                    Ztype[x2][y2]=99;
                }else if(ifZ0[x2][y2]==2){
                    //g.drawImage(Z3t,x2,y2-40,null);
                    Ztype[x2][y2]=39;
                }
            }
        }
       /*public void DrawPea(int x1,int y1,int type){
            try {
                Pea=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Plants\\dou.gif"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            x=x1;
            y=y1;
            Thread aThread = new Thread();
            for(;x<(1920-Game.X_OFFSET);x=x+30) {
                    aThread =new Thread(new Runnable(){
                        Object lock=new Object();
                        public void run() {
                            while (true){synchronized (lock){
                            Graphics g=getGraphics();
                            g.drawImage(Pea,x,y,null);
                            if(type==2){
                                g.drawImage(Pea,x-40,y,null);
                            }
                                //try {
                                    Thread.sleep(100);
                                //} catch (InterruptedException e) {
                                    e.printStackTrace();
                               // }
                                //p1.repaint();
                                }
                        }}
                    });
                    aThread.start();
                    try{
                        p1.repaint();
                    aThread.sleep(150);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
            //for(;x<(1920-Game.X_OFFSET);x=x+30) {
               // Graphics g=getGraphics();
               // g.drawImage(Pea,x,y,null);
               // p1.repaint();
            //}
        }*/
        public void paint(Graphics g) {
            // 画背景
            g.drawImage(rungame,0,0,null);
            //选择框
            g.drawImage(SeedBank,220,0,null);
            //显示阳光数
            g.drawString(sun,252,75);
            try {
                SunFlower=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\SunFlower0.png"));
                Peashooter=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\Peashooter0.png"));
                WallNut=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\WallNut0.png"));
                Reshooter=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\Repeater0.png"));
                CherryBomb=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\CherryBomb0.png"));
                PotatoMine=ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Cards\\PotatoMine0.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            //画选择卡背景图
            g.drawImage(SunFlower,300,7,null);
            g.drawImage(Peashooter,360,7,null);
            g.drawImage(WallNut,420,7,null);
            g.drawImage(Reshooter,480,7,null);
            g.drawImage(CherryBomb,540,7,null);
            g.drawImage(PotatoMine,600,7,null);
            //重绘按钮
            SunF.requestFocus();
            Peas.requestFocus();
            WallN.requestFocus();
            Reap.requestFocus();
            Cherry.requestFocus();
            Potato.requestFocus();
            if (lkl == 1) {
                JBsun.requestFocus();
            }
            /*
            //绘制选择框(鼠标移动)
            if(selected_X>0&&selected_Y>0){
            g.drawImage(selected,selected_X*RATE+X_OFFSET,selected_Y*RATE+Y_OFFSET,null);
            }
            */
            //画豌豆
            if(ifPeas[PeaX][PeaY]==1){
                g.drawImage(Pea,PeaX,PeaY,null);
                if(PeaType==2){
                    g.drawImage(Pea,PeaX-40,PeaY,null);
                }
            }
            //画僵尸，需优化
            for (int Sizex=0;Sizex<1920;Sizex++){
                for(int Sizey=0;Sizey<800;Sizey++){
            if(ifZ0[Sizex][Sizey]==1||ifZ0[Sizex][Sizey]==2||ifZ0[Sizex][Sizey]==3) {//判断中1为僵尸正在行走，2为正在攻击，3为死亡
                if (Ztype[Sizex][Sizey] == 10) {
                    g.drawImage(Z10, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 11) {
                    g.drawImage(Z11, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 12) {
                    g.drawImage(Z12, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 13) {
                    g.drawImage(Z13, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 14) {
                    g.drawImage(Z14, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 20) {
                    g.drawImage(Z20, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 21) {
                    g.drawImage(Z21, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 22) {
                    g.drawImage(Z22, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 23) {
                    g.drawImage(Z23, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 24) {
                    g.drawImage(Z24, Sizex, Sizey - 40, null);
                }else if (Ztype[Sizex][Sizey] == 29) {
                    g.drawImage(Z2t, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 30) {
                    g.drawImage(Z30, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 31) {
                    g.drawImage(Z31, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 32) {
                    g.drawImage(Z32, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 33) {
                    g.drawImage(Z33, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 34) {
                    g.drawImage(Z34, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 39) {
                    g.drawImage(Z3t, Sizex, Sizey - 40, null);
                } else if (Ztype[Sizex][Sizey] == 99) {
                    g.drawImage(Zd, Sizex, Sizey - 40, null);
                }
                Ztype[Sizex][Sizey]=0;
            }
            }
            }
            //if(ifZ0[][])
           for(int i=0;i<9;i++){
               for(int j=0;j<5;j++){
                   if(plants[i][j]==1){
                    //添加一个向日葵
                    BufferedImage plant1;
                    if(state[i][j]!=1){
                    SunFlower pl1=new SunFlower();
                        PBlood=pl1.Blood;
                        pl1.Skill(i * X_RATE + X_OFFSET,j * Y_RATE + Y_OFFSET+20);
                        Game.ifplants[i * X_RATE + X_OFFSET][j * Y_RATE + Y_OFFSET]=1;
                        state[i][j]=1;
                    }
                    if(PBlood>0) {
                        try {
                            plant1 = ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Plants\\sunflower1.gif"));
                            g.drawImage(plant1, i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET, null);
                            g.drawString(sun, 252, 75);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }else {
                        state[i][j]=0;
                        plants[i][j]=0;
                    }
                   }
                   if(plants[i][j]==2){
                       BufferedImage plant1;
                       if(state[i][j]!=1){
                           Peashooter pl1=new Peashooter();
                           PBlood=pl1.Blood;
                           if(ifZ[j]==0){
                           pl1.Skill(i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET);
                           Game.ifplants[i * X_RATE + X_OFFSET][j * Y_RATE + Y_OFFSET]=1;
                           }
                           state[i][j]=1;
                       }
                       if(PBlood>0) {
                           try {
                               plant1 = ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Plants\\pea1.gif"));
                               g.drawImage(plant1, i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET, null);
                               g.drawString(sun, 252, 75);
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }else {
                           state[i][j]=0;
                           plants[i][j]=0;
                       }
                   }
                   if(plants[i][j]==3){
                       BufferedImage plant1;
                       if(state[i][j]!=1) {
                           WallNut pl1 = new WallNut();
                           PBlood=pl1.Blood;
                           pl1.Skill();
                           Game.ifplants[i * X_RATE + X_OFFSET][j * Y_RATE + Y_OFFSET]=1;
                       }
                       if(PBlood>0) {
                           try {
                               plant1 = ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Plants\\wall1.gif"));
                               g.drawImage(plant1, i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET, null);
                               g.drawString(sun, 252, 75);
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }else {
                           state[i][j]=0;
                           plants[i][j]=0;
                       }
                   }
                   if(plants[i][j]==4){
                       BufferedImage plant1;
                       if(state[i][j]!=1) {
                           RePeater pl1 = new RePeater();
                           PBlood=pl1.Blood;
                           if(ifZ[j]==0){
                               pl1.Skill(i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET);
                               Game.ifplants[i * X_RATE + X_OFFSET][j * Y_RATE + Y_OFFSET]=1;
                           }
                           state[i][j]=1;
                       }
                       if(PBlood>0) {
                           try {
                               plant1 = ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Plants\\Repeater_6.png"));
                               g.drawImage(plant1, i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET, null);
                               g.drawString(sun, 252, 75);
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }else {
                           state[i][j]=0;
                           plants[i][j]=0;
                       }
                   }
                   if(plants[i][j]==5){
                       BufferedImage plant1;
                       if(state[i][j]!=1) {
                           CherryBomb pl1 = new CherryBomb();
                           PBlood=pl1.Blood;
                           pl1.Skill();
                           Game.ifplants[i * X_RATE + X_OFFSET][j * Y_RATE + Y_OFFSET]=1;
                       }
                       if(PBlood>0) {
                           try {
                               plant1 = ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Plants\\cherry1.gif"));
                               g.drawImage(plant1, i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET, null);
                               g.drawString(sun, 252, 75);
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }else {
                           state[i][j]=0;
                           plants[i][j]=0;
                       }
                   }
                   if(plants[i][j]==6){
                       BufferedImage plant1;
                       if(state[i][j]!=1) {
                           PotatoMine pl1 = new PotatoMine();
                           PBlood=pl1.Blood;
                           pl1.Skill();
                           Game.ifplants[i * X_RATE + X_OFFSET][j * Y_RATE + Y_OFFSET]=1;
                       }
                       if(PBlood>0) {
                           try {
                               //没有找到缩在土里的图片，直接准备就绪
                               plant1 = ImageIO.read(new File("D:\\Morven\\IdeaProjects\\PVZ\\picture\\Plants\\tobo.gif"));
                               g.drawImage(plant1, i * X_RATE + X_OFFSET, j * Y_RATE + Y_OFFSET, null);
                               g.drawString(sun, 252, 75);
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }
                       else {
                           state[i][j]=0;
                           plants[i][j]=0;
                       }
                   }
                   //每放一个植物都会刷新一次界面，当点铲子时type变为其他值，重绘时此处将不会产生图片，也就实现了删除
               }
            }
            }
        }
    }